﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text.RegularExpressions;
using System.Web;
using System.Web.Mvc;
using AncientLand.Common;
using AncientLand.Service.Entity;
using AncientLand.Service.IBiz;

namespace AncientLand.Controllers
{
    public class HomeController : Controller
    {
       private readonly IPhotosBiz _photosService;

       public HomeController(IPhotosBiz photosService)
        {
            this._photosService = photosService;
        }

        public ActionResult Index()
        {

           return View(_photosService.GetItemsByPagesForHomePage(1,10));
        }

       



        [HttpPost]
        public ActionResult Upload()
        {
            const int maxAttachSize = 2097152;
            var dateTimeString=DateTime.Now.ToString("yyyyMMdd");
            var upFolder = "~/UpFiles/News/" + dateTimeString+"/";
            var uploadPath = Server.MapPath(upFolder );
            var errorMsg = string.Empty;
            var fileMsg = string.Empty;
            var imagePath = string.Empty;
            var strFileName = string.Empty;
            var disposition = Request.ServerVariables["HTTP_CONTENT_DISPOSITION"];
            byte[] fileContent; // 统一转换为byte数组处理
            try
            {
                if (disposition != null)
                {
                    // HTML5上传
                    fileContent = Request.BinaryRead(Request.TotalBytes);
                    strFileName = Regex.Match(disposition, "filename=\"(.+?)\"").Groups[1].Value;// 读取原始文件名
                }
                else
                {
                    // 初始化byte长度.
                    HttpPostedFileBase file = Request.Files["Filedata"];
                    strFileName =  file.FileName;
                    fileContent = new Byte[file.ContentLength];
                    // 转换为byte类型
                    System.IO.Stream stream = file.InputStream;
                    stream.Read(fileContent, 0, file.ContentLength);
                    stream.Close();
                }
                if (fileContent.Length == 0)
                    {
                        errorMsg = "无数据提交";
                    }
                    else
                    {
                        if (fileContent.Length > maxAttachSize)
                        {
                            errorMsg = "文件大小超过" + maxAttachSize + "字节";
                        }
                        else
                        {
                           
                            // string strTimeInfo = DateTime.Now.ToString("yyyyMMddhhmmssfff");
                            //strFileName = strTimeInfo + "-" + strFileName;
                            imagePath = uploadPath + strFileName;
                            if (!Directory.Exists(uploadPath))
                            {
                                Directory.CreateDirectory(uploadPath);
                            }
                            var fs = new System.IO.FileStream(imagePath, System.IO.FileMode.Create, System.IO.FileAccess.Write);
                            fs.Write(fileContent, 0, fileContent.Length);
                            fs.Flush();
                            fs.Close();

                            fileMsg = Url.Content(upFolder + strFileName);
                        }
                    }
                   
            }
            catch (Exception ex)
            {
                errorMsg = ex.Message;
                Logger.Instance.Error(ex);
            }
            var jsonObject = new UpLoadMsg {err = errorMsg, msg = fileMsg};
            return this.Content(SerializationHelper.GetJson(jsonObject));
        }
    }
}
